/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package simplemessenger;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Bill
 */

///This is where the meat of the program is...
///both the client and server configuration use this class to handle in/out bound messages
class SendReceive {
    //var declaration
    Socket connection; 
    PrintWriter pw;
    BufferedReader br;

    public SendReceive(Socket connection)  {
        try {
            //System.out.println("Entered SendReceive");
            this.connection = connection;//pass parameter to ivar
            pw = new PrintWriter(this.connection.getOutputStream());//create printwriter with the connection that was just passed to us
            br = new BufferedReader(new InputStreamReader(this.connection.getInputStream())); //create a buffered reader with the connection just passed to us as well
            Send s = new Send(pw); //create an instance of Send (inner-class)
            Receive r = new Receive(br); //create an instance of Receive (inner-class)
        } catch (IOException ex) {
            Logger.getLogger(SendReceive.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    private class Send implements Runnable /*runnable makes the class threaded*/ {
        
        PrintWriter pw;//ivar for storing passed value
        Thread t = new Thread(this, "SendThread"); //thread object
        
        public Send(PrintWriter pw){
            //System.out.println("In Send");
            this.pw = pw;
            startThread();
        }

        private void startThread(){//starts the thread
            t.start();
        }
        
        @Override
        public void run() {
            Scanner s = new Scanner(System.in);
            while(true){
                pw.println(s.nextLine());//write data to the printwriter
                pw.flush();//flush the printwriter clearing the buffer and sending all data in buffer
            }
        }
    }

    private class Receive implements Runnable {
        
        BufferedReader br;//ivar for storing passed value
        Thread t = new Thread(this, "ReceiveThread");//thread object
        
        public Receive(BufferedReader br){
            //System.out.println("In Receive");
            this.br = br;
            startThread();
        }
        
        @Override
        public void run() {
            while(true){
                try {
                    System.out.println(br.readLine());//wait for something to come in on the wire and print it when it does
                } catch (IOException ex) {
                    Logger.getLogger(SendReceive.class.getName()).log(Level.SEVERE, null, ex);
                }
                
            }
        }

        private void startThread() {
            t.start();
        }
    }
}
